package Integrity;

/*
 * NONE   : No error
 * DIC101 : Data Integrity fails addition checking
 * DIC102 : Data Integrity fails VAT calculation checking
 * DIC103 : Data Integrity fails Duplicate data checking of closing balance and total due
 * DIC104 : Data Integrity fails addition checking of Telco companies new charges
 * DIC105 : Data Integrity fails addition checking of Municipality companies new charges
 */
public class Data_Int_Checks {
	
	public Data_Int_Checks(Xml_Extracted_Data xed){
		payment_received = xed.get_scraped_pay_rec();
		opening_balance = xed.get_scraped_op_bal();
		VAT_amount = xed.get_scraped_vat_amount();
		call_charges = xed.get_scraped_call_chrg();
		new_charges = xed.get_scraped_call_chrg();
		service_charges = xed.get_scraped_serv_chrg();
		deductions = xed.get_scraped_ded();
		discount = xed.get_scraped_disc();
		total_due = xed.get_scraped_t_due();
		closing_balance = xed.get_scraped_cl_bal();
		service_type = xed.get_scraped_type();
	}
	
	public String getErrorCode(){
		if (additionChecking()) error_code = "DIC001";
		else if (VAT_Calculations()) error_code = "DIC002";
		else if (closingBalance_Duplicate_Data()) error_code = "DIC003";
		else if (service_type.equals("telco") && newCharges_Telco()) error_code = "DIC004";
		else if (service_type.equals("municipality") && newCharges_Munic()) error_code = "DIC005";
		else error_code = "NONE";
		return error_code;
	}
	
	public Boolean additionChecking(){
		
		if (Math.abs(opening_balance - payment_received + new_charges - discount - deductions - total_due) <= allowed_deviation) {
		return true;
		}
		return false;
	}
	
	public Boolean VAT_Calculations(){
		double charges_without_VAT = new_charges - VAT_amount;
		if (Math.abs((charges_without_VAT * 0.14) - VAT_amount) <= allowed_deviation) {
			return true;
		}
		return false;
	}
	
	public Boolean closingBalance_Duplicate_Data(){
		
		if (closing_balance == total_due) {
		return true;
		}
		return false;
	}
	
	public Boolean newCharges_Telco(){
		
		if ((call_charges + service_charges - new_charges)<= allowed_deviation) {
		return true;
		}
		return false;
	}
	
	public Boolean newCharges_Munic(){
		
		if ((elect_charges + water_charges + sew_charges + ref_charges - new_charges)<= allowed_deviation) {
		return true;
		}
		return false;
	}
	
	private double payment_received;
	private double opening_balance;
	private double VAT_amount;
	private double call_charges;
	private double new_charges;
	private double service_charges;
	private double deductions;
	private double discount;
	private double total_due;
	private double closing_balance;
	private double elect_charges;
	private double water_charges;
	private double sew_charges;
	private double ref_charges;
    private String service_type = "";
    private String error_code = "";
	
	final double allowed_deviation = 0.01;

}
